package com.browan.demo;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;

import com.browan.freeppmobile.jni.FreeppKit;

public class DaemonService extends Service{
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		FreeppKit.getInstance().register();
		
		IntentFilter filter = new IntentFilter();
		filter.addAction("iax.new.call");
		filter.addAction("iax.hangup");
		
		IaxReceiver receiver = new IaxReceiver();
		this.registerReceiver(receiver, filter);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		FreeppKit.getInstance().unregister();
	}
	
	private class IaxReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			
			if(action.equals("iax.hangup")) {
				Intent hangup = new Intent("UI.HANGUP");
				DaemonService.this.sendBroadcast(hangup);
				
				FreeppKit.inMeeting = false;
			}
			else if(action.equals("iax.new.call")) {
				FreeppKit.getInstance().answer();
				FreeppKit.inMeeting = true;
				
				Intent meeting = new Intent(DaemonService.this, MeetingActivity.class);
				meeting.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				DaemonService.this.startActivity(meeting);
			}
			
		}
		
	}
}
